package org.flexharmony.eclipse.nature;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.actions.ActionDelegate;

public class RemoveProjectNatureAction extends ActionDelegate implements IWorkbenchWindowActionDelegate
{
  private IProject project;
  
  public RemoveProjectNatureAction()
  {}
  
  public void init(IWorkbenchWindow window)
  {}
  
  public void selectionChanged(IAction action, ISelection selection)
  {
    IStructuredSelection structuredSelection = (IStructuredSelection)selection;
    Object element = structuredSelection.getFirstElement();
    project = (IProject)element;
  }
  
  public void run(IAction action)
  {
    try
    {
      IProjectDescription projectDescription = project.getDescription();
      List natureIds = new ArrayList();
      natureIds.addAll(Arrays.asList(projectDescription.getNatureIds()));
      natureIds.remove(HarmonyProjectNature.NATURE_ID);
      
      projectDescription.setNatureIds((String[])natureIds.toArray(new String[natureIds.size()]));
      project.setDescription(projectDescription, null);
    }
    catch (CoreException cexc)
    {
      cexc.printStackTrace();
    }
  }
}
